# Justfile for COS Demo Project
# Run `just --list` to see all available commands

# Default recipe to display help
default:
    @just --list

# Variables
binary_name := "cos"
cli_cmd := "./cmd/cos-cli"
gui_cmd := "./cmd/cos-gui"
bin_dir := "./bin"
dist_dir := "./dist"

set windows-powershell := true

# Build CLI binary
build-cli:
    @echo "Building CLI..."
    @mkdir -p {{bin_dir}}
    go build -o {{bin_dir}}/{{binary_name}} {{cli_cmd}}
    @echo "✓ CLI built: {{bin_dir}}/{{binary_name}}"

# Build GUI binary
build-gui:
    @echo "Building GUI..."
    @mkdir -p {{bin_dir}}
    go build -o {{bin_dir}}/{{binary_name}}-gui {{gui_cmd}}
    @echo "✓ GUI built: {{bin_dir}}/{{binary_name}}-gui"

# Build all binaries
build: build-cli build-gui
    @echo "✓ All binaries built"

# Build with race detector (for debugging)
build-debug:
    @echo "Building with race detector..."
    @mkdir -p {{bin_dir}}
    go build -race -o {{bin_dir}}/{{binary_name}}-debug {{cli_cmd}}
    @echo "✓ Debug build: {{bin_dir}}/{{binary_name}}-debug"

# Build Windows GUI (requires mingw-w64)
build-windows-gui:
    @echo "Building Windows GUI..."
    @mkdir -p {{dist_dir}}
    CGO_ENABLED=1 GOOS=windows GOARCH=amd64 \
        CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ \
        go build -o {{dist_dir}}/{{binary_name}}-gui.exe {{gui_cmd}}
    @echo "✓ Windows GUI built: {{dist_dir}}/{{binary_name}}-gui.exe"

# Run CLI with arguments (e.g., just run-cli list)
run-cli *ARGS:
    @go run {{cli_cmd}} {{ARGS}}

# Run GUI
run-gui:
    @go run {{gui_cmd}}

# Run tests
test:
    @echo "Running tests..."
    go test -v ./...

# Run tests with coverage
test-coverage:
    @echo "Running tests with coverage..."
    go test -v -coverprofile=coverage.out ./...
    go tool cover -html=coverage.out -o coverage.html
    @echo "✓ Coverage report: coverage.html"

# Run linter
lint:
    @echo "Running linter..."
    golangci-lint run ./...

# Format code
fmt:
    @echo "Formatting code..."
    go fmt ./...
    @echo "✓ Code formatted"

# Install CLI to system (requires sudo on Unix)
install: build-cli
    @python3 scripts/install.py --system --cli

# Install CLI to user directory
install-user: build-cli
    @python3 scripts/install.py --user --cli

# Install GUI to system (requires sudo on Unix)
install-gui: build-gui
    @python3 scripts/install.py --system --gui

# Install GUI to user directory
install-gui-user: build-gui
    @python3 scripts/install.py --user --gui

# Install both CLI and GUI to system
install-all: build
    @python3 scripts/install.py --system --cli
    @python3 scripts/install.py --system --gui

# Install both CLI and GUI to user directory
install-all-user: build
    @python3 scripts/install.py --user --cli
    @python3 scripts/install.py --user --gui

# Uninstall CLI from system
uninstall:
    @python3 scripts/install.py --uninstall --system --cli

# Uninstall CLI from user directory
uninstall-user:
    @python3 scripts/install.py --uninstall --user --cli

# Uninstall GUI from system
uninstall-gui:
    @python3 scripts/install.py --uninstall --system --gui

# Uninstall GUI from user directory
uninstall-gui-user:
    @python3 scripts/install.py --uninstall --user --gui

# Uninstall both CLI and GUI from system
uninstall-all:
    @python3 scripts/install.py --uninstall --system --cli
    @python3 scripts/install.py --uninstall --system --gui

# Uninstall both CLI and GUI from user directory
uninstall-all-user:
    @python3 scripts/install.py --uninstall --user --cli
    @python3 scripts/install.py --uninstall --user --gui

# Clean build artifacts
clean:
    @echo "Cleaning build artifacts..."
    @rm -rf {{bin_dir}} {{dist_dir}}
    @rm -f coverage.out coverage.html
    @echo "✓ Clean complete"

# Install dependencies
deps:
    @echo "Installing dependencies..."
    go mod download
    go mod tidy
    @echo "✓ Dependencies installed"

# Quick upload test (upload a test file)
test-upload FILE:
    @{{bin_dir}}/{{binary_name}} upload {{FILE}}

# List files in COS
test-list:
    @{{bin_dir}}/{{binary_name}} -action list

# Development setup
dev-setup:
    @echo "Setting up development environment..."
    @just deps
    @just build
    @echo "✓ Development setup complete"
    @echo ""
    @echo "Try: just run-cli upload <file>"

# Show project info
info:
    @echo "COS Demo Project"
    @echo "================"
    @echo "Go version: $(go version)"
    @echo "Binary dir: {{bin_dir}}"
    @echo "Dist dir: {{dist_dir}}"
    @echo ""
    @echo "Build Commands:"
    @echo "  just build              - Build all binaries (CLI + GUI)"
    @echo "  just build-cli          - Build CLI only"
    @echo "  just build-gui          - Build GUI only"
    @echo "  just build-debug        - Build with race detector"
    @echo ""
    @echo "Installation Commands (CLI):"
    @echo "  just install            - Install CLI to system (needs sudo)"
    @echo "  just install-user       - Install CLI to user directory"
    @echo "  just uninstall          - Uninstall CLI from system"
    @echo "  just uninstall-user     - Uninstall CLI from user directory"
    @echo ""
    @echo "Installation Commands (GUI):"
    @echo "  just install-gui        - Install GUI to system (needs sudo)"
    @echo "  just install-gui-user   - Install GUI to user directory"
    @echo "  just uninstall-gui      - Uninstall GUI from system"
    @echo "  just uninstall-gui-user - Uninstall GUI from user directory"
    @echo ""
    @echo "Installation Commands (Both):"
    @echo "  just install-all        - Install CLI + GUI to system"
    @echo "  just install-all-user   - Install CLI + GUI to user directory"
    @echo "  just uninstall-all      - Uninstall CLI + GUI from system"
    @echo "  just uninstall-all-user - Uninstall CLI + GUI from user directory"
    @echo ""
    @echo "Run Commands:"
    @echo "  just run-cli <args>     - Run CLI without building"
    @echo "  just run-gui            - Run GUI without building"
    @echo ""
    @echo "Test Commands:"
    @echo "  just test               - Run tests"
    @echo "  just test-coverage      - Run tests with coverage"
    @echo "  just fmt                - Format code"
    @echo "  just lint               - Run linter"
    @echo ""
    @echo "Other Commands:"
    @echo "  just clean              - Clean build artifacts"
    @echo "  just deps               - Install/update dependencies"
    @echo "  just dev-setup          - Setup development environment"
    @echo ""
    @echo "Run 'just --list' to see all available commands"
